//
//  GQHContractExpenseInfoController.m
//  Xanthium
//
//  Created by GuanQinghao on 18/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHContractExpenseInfoView.h"

#pragma mark Controller
#import "GQHContractExpenseInfoController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHContractExpenseInfoController () <GQHContractExpenseInfoViewDelegate> {
    
#pragma mark Data
    NSMutableDictionary *parameters;
#pragma mark View
    /// 自定义根视图
    GQHContractExpenseInfoView *rootView;
}

@end


@implementation GQHContractExpenseInfoController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHContractExpenseInfoView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    NSString *freightWeight = [NSString stringWithFormat:@"%.2f",_qh_qutationModel.qh_freightWeight];
    [rootView.qh_contentArray qh_safetySetObject:freightWeight atIndex:0];
    
    NSString *goodsFee = [NSString stringWithFormat:@"%.2f",_qh_qutationModel.qh_amountValue];
    [rootView.qh_contentArray qh_safetySetObject:goodsFee atIndex:1];
    [rootView.qh_tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"协议费用信息";
    [self.qh_rightButton setTitle:@"提交" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
            
        case 0: {
            
            switch (indexPath.row) {
                    
                case 0: {
                    
                    /// 货物重量
                    [self qh_alertWithTitle:@"请输入货物重量" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"货物重量";
                        textField.keyboardType = UIKeyboardTypeNumberPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:0];
                        [rootView.qh_tableView reloadData];
                        
                    } completion:nil];
                }
                    break;
                case 1: {
                    
                    /// 货物体积
                    [self qh_alertWithTitle:@"请输入货物体积" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"货物体积";
                        textField.keyboardType = UIKeyboardTypeNumberPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:1];
                        [rootView.qh_tableView reloadData];
                        
                    } completion:nil];
                }
                    break;
                case 2: {
                    
                    /// 货物数量
                    [self qh_alertWithTitle:@"请输入货物数量" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"货物数量";
                        textField.keyboardType = UIKeyboardTypeNumberPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:2];
                        [rootView.qh_tableView reloadData];
                        
                    } completion:nil];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 1: {
            
            switch (indexPath.row) {
                    
                case 0: {
                    
                    /// 油卡
                    [self qh_alertWithTitle:@"请输入油卡金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"油卡金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        /// 所列费用
                        float preGas = [inputTextField.text floatValue];
                        float preETC = [rootView.qh_contentArray[4] floatValue];
                        float preCash = [rootView.qh_contentArray[5] floatValue];
                        float postGas = [rootView.qh_contentArray[7] floatValue];
                        float postETC = [rootView.qh_contentArray[8] floatValue];
                        float postCash = [rootView.qh_contentArray[9] floatValue];
                        /// 预付款和尾款
                        float preTotal = preGas + preETC + preCash;
                        float postTotal = postGas + postETC +postCash;
                        /// 总计
                        float total = preTotal + postTotal;
                        
                        /// 预付款和总计更新
                        NSString *preTotalString = [NSString stringWithFormat:@"%.2f",preTotal];
                        [rootView.qh_contentArray qh_safetySetObject:preTotalString atIndex:6];
                        NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                        [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:11];
                        
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:3];
                        [rootView.qh_tableView reloadData];
                    } completion:nil];
                }
                    break;
                case 1: {
                    
                    /// ETC
                    [self qh_alertWithTitle:@"请输入ETC金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"ETC金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        
                        if(inputTextField.text && inputTextField.text.length >0) {
                            
                            /// 所列费用
                            float preGas = [rootView.qh_contentArray[3] floatValue];
                            float preETC = [inputTextField.text floatValue];
                            float preCash = [rootView.qh_contentArray[5] floatValue];
                            float postGas = [rootView.qh_contentArray[7] floatValue];
                            float postETC = [rootView.qh_contentArray[8] floatValue];
                            float postCash = [rootView.qh_contentArray[9] floatValue];
                            /// 预付款和尾款
                            float preTotal = preGas + preETC + preCash;
                            float postTotal = postGas + postETC +postCash;
                            /// 总计
                            float total = preTotal + postTotal;
                            
                            /// 预付款和总计更新
                            NSString *preTotalString = [NSString stringWithFormat:@"%.2f",preTotal];
                            [rootView.qh_contentArray qh_safetySetObject:preTotalString atIndex:6];
                            NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                            [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:11];
                            
                            [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:4];
                            [rootView.qh_tableView reloadData];
                        }
                        
                    } completion:nil];
                }
                    break;
                case 2: {
                    
                    /// 现金
                    [self qh_alertWithTitle:@"请输入现金金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"现金金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        
                        if (inputTextField.text && inputTextField.text.length > 0) {
                            
                            /// 所列费用
                            float preGas = [rootView.qh_contentArray[3] floatValue];
                            float preETC = [rootView.qh_contentArray[4] floatValue];
                            float preCash = [inputTextField.text floatValue];
                            float postGas = [rootView.qh_contentArray[7] floatValue];
                            float postETC = [rootView.qh_contentArray[8] floatValue];
                            float postCash = [rootView.qh_contentArray[9] floatValue];
                            /// 预付款和尾款
                            float preTotal = preGas + preETC + preCash;
                            float postTotal = postGas + postETC +postCash;
                            /// 总计
                            float total = preTotal + postTotal;
                            
                            /// 预付款和总计更新
                            NSString *preTotalString = [NSString stringWithFormat:@"%.2f",preTotal];
                            [rootView.qh_contentArray qh_safetySetObject:preTotalString atIndex:6];
                            NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                            [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:11];
                            
                            [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:5];
                            [rootView.qh_tableView reloadData];
                        }
                    } completion:nil];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 2: {
            switch (indexPath.row) {
                    
                case 0: {
                    
                    /// 油卡
                    [self qh_alertWithTitle:@"请输入油卡金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"油卡金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        if (inputTextField.text && inputTextField.text.length > 0) {
                            
                            /// 所列费用
                            float preGas = [rootView.qh_contentArray[3] floatValue];
                            float preETC = [rootView.qh_contentArray[4] floatValue];
                            float preCash = [rootView.qh_contentArray[5] floatValue];
                            float postGas = [inputTextField.text floatValue];
                            float postETC = [rootView.qh_contentArray[8] floatValue];
                            float postCash = [rootView.qh_contentArray[9] floatValue];
                            /// 预付款和尾款
                            float preTotal = preGas + preETC + preCash;
                            float postTotal = postGas + postETC +postCash;
                            /// 总计
                            float total = preTotal + postTotal;
                            
                            /// 预付款和总计更新
                            NSString *postTotalString = [NSString stringWithFormat:@"%.2f",postTotal];
                            [rootView.qh_contentArray qh_safetySetObject:postTotalString atIndex:10];
                            NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                            [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:11];
                            
                            [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:7];
                            [rootView.qh_tableView reloadData];
                        }
                    } completion:nil];
                }
                    break;
                case 1: {
                    
                    /// ETC
                    [self qh_alertWithTitle:@"请输入ETC金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"ETC金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        if (inputTextField.text && inputTextField.text.length > 0) {
                            
                            /// 所列费用
                            float preGas = [rootView.qh_contentArray[3] floatValue];
                            float preETC = [rootView.qh_contentArray[4] floatValue];
                            float preCash = [rootView.qh_contentArray[5] floatValue];
                            float postGas = [rootView.qh_contentArray[7] floatValue];
                            float postETC = [inputTextField.text floatValue];
                            float postCash = [rootView.qh_contentArray[9] floatValue];
                            /// 预付款和尾款
                            float preTotal = preGas + preETC + preCash;
                            float postTotal = postGas + postETC +postCash;
                            /// 总计
                            float total = preTotal + postTotal;
                            
                            /// 预付款和总计更新
                            NSString *postTotalString = [NSString stringWithFormat:@"%.2f",postTotal];
                            [rootView.qh_contentArray qh_safetySetObject:postTotalString atIndex:10];
                            NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                            [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:11];
                            
                            [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:8];
                            [rootView.qh_tableView reloadData];
                        }
                    } completion:nil];
                }
                    break;
                case 2: {
                    
                    /// 现金
                    [self qh_alertWithTitle:@"请输入现金金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"现金金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        if (inputTextField.text && inputTextField.text.length > 0) {
                            
                            /// 所列费用
                            float preGas = [rootView.qh_contentArray[3] floatValue];
                            float preETC = [rootView.qh_contentArray[4] floatValue];
                            float preCash = [rootView.qh_contentArray[5] floatValue];
                            float postGas = [rootView.qh_contentArray[7] floatValue];
                            float postETC = [rootView.qh_contentArray[8] floatValue];
                            float postCash = [inputTextField.text floatValue];
                            /// 预付款和尾款
                            float preTotal = preGas + preETC + preCash;
                            float postTotal = postGas + postETC +postCash;
                            /// 总计
                            float total = preTotal + postTotal;
                            
                            /// 预付款和总计更新
                            NSString *postTotalString = [NSString stringWithFormat:@"%.2f",postTotal];
                            [rootView.qh_contentArray qh_safetySetObject:postTotalString atIndex:10];
                            NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                            [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:11];
                            
                            [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:9];
                            [rootView.qh_tableView reloadData];
                        }
                    } completion:nil];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender
{
    for (int i = 0; i < rootView.qh_contentArray.count; i ++)
    {
        NSString * str = rootView.qh_contentArray[i];
        if ([str isEqualToString:@""])
        {
            [rootView.qh_contentArray replaceObjectAtIndex:i withObject:@"0"];
        }
    }
    
    NSString * tailMoney = rootView.qh_contentArray[10];
    if (tailMoney.floatValue == 0)
    {
        [self showToastWithText:@"总金额合计不能为0"];
        return;
    }
    
    NSMutableDictionary *jsonDictionary = [NSMutableDictionary dictionary];
    [jsonDictionary setObject: _qh_contractID forKey:@"waybillAgreementId"];
    [jsonDictionary setObject:rootView.qh_contentArray[0] forKey:@"demandVehicle"];
    
    NSMutableArray *prepaymentArray = [NSMutableArray array];
    NSMutableDictionary *preGasDictionary = [NSMutableDictionary dictionary];
    [preGasDictionary setObject:@"01" forKey:@"paymentType"];
    [preGasDictionary setObject:rootView.qh_contentArray[2] forKey:@"amount"];
    [preGasDictionary setObject:@"02" forKey:@"paymentTerms"];
    [preGasDictionary setObject:@"" forKey:@"reserved1"];
    [prepaymentArray addObject:preGasDictionary];
    
    NSMutableDictionary *preETCDictionary = [NSMutableDictionary dictionary];
    [preETCDictionary setObject:@"02" forKey:@"paymentType"];
    [preETCDictionary setObject:rootView.qh_contentArray[3] forKey:@"amount"];
    [preETCDictionary setObject:@"02" forKey:@"paymentTerms"];
    [preETCDictionary setObject:@"" forKey:@"reserved1"];
    [prepaymentArray addObject:preETCDictionary];
    
    NSMutableDictionary *preCashDictionary = [NSMutableDictionary dictionary];
    [preCashDictionary setObject:@"03" forKey:@"paymentType"];
    [preCashDictionary setObject:rootView.qh_contentArray[4] forKey:@"amount"];
    [preCashDictionary setObject:@"02" forKey:@"paymentTerms"];
    [preCashDictionary setObject:@"" forKey:@"reserved1"];
    [prepaymentArray addObject:preCashDictionary];
    
    [jsonDictionary setObject:prepaymentArray forKey:@"FreightPrepaid"];
    
    NSMutableArray *postpaymentArray = [NSMutableArray array];
    NSMutableDictionary *postGasDictionary = [NSMutableDictionary dictionary];
    [postGasDictionary setObject:@"01" forKey:@"paymentType"];
    [postGasDictionary setObject:rootView.qh_contentArray[6] forKey:@"amount"];
    [postGasDictionary setObject:@"02" forKey:@"paymentTerms"];
    [postGasDictionary setObject:@"" forKey:@"reserved1"];
    [postpaymentArray addObject:postGasDictionary];
    
    NSMutableDictionary *postETCDictionary = [NSMutableDictionary dictionary];
    [postETCDictionary setObject:@"02" forKey:@"paymentType"];
    [postETCDictionary setObject:rootView.qh_contentArray[7] forKey:@"amount"];
    [postETCDictionary setObject:@"02" forKey:@"paymentTerms"];
    [postETCDictionary setObject:@"" forKey:@"reserved1"];
    [postpaymentArray addObject:postETCDictionary];
    
    NSMutableDictionary *postCashDictionary = [NSMutableDictionary dictionary];
    [postCashDictionary setObject:@"03" forKey:@"paymentType"];
    [postCashDictionary setObject:rootView.qh_contentArray[8] forKey:@"amount"];
    [postCashDictionary setObject:@"02" forKey:@"paymentTerms"];
    [postCashDictionary setObject:@"" forKey:@"reserved1"];
    [postpaymentArray addObject:postCashDictionary];
    
    [jsonDictionary setObject:postpaymentArray forKey:@"FreightTailed"];
    
    NSMutableArray *otherArray = [NSMutableArray array];
    NSMutableDictionary *otherDictionary = [NSMutableDictionary dictionary];
    [otherDictionary setObject:@"01" forKey:@"paymentType"];
    [otherDictionary setObject:@"0" forKey:@"amount"];
    [otherDictionary setObject:@"02" forKey:@"paymentTerms"];
    [otherDictionary setObject:@"" forKey:@"reserved1"];
    [otherArray addObject:otherDictionary];
    
    NSMutableDictionary *otherDictionary1 = [NSMutableDictionary dictionary];
    [otherDictionary1 setObject:@"02" forKey:@"paymentType"];
    [otherDictionary1 setObject:@"0" forKey:@"amount"];
    [otherDictionary1 setObject:@"02" forKey:@"paymentTerms"];
    [otherDictionary1 setObject:@"" forKey:@"reserved1"];
    [otherArray addObject:otherDictionary1];
    
    NSMutableDictionary *otherDictionary2 = [NSMutableDictionary dictionary];
    [otherDictionary2 setObject:@"03" forKey:@"paymentType"];
    [otherDictionary2 setObject:@"0" forKey:@"amount"];
    [otherDictionary2 setObject:@"02" forKey:@"paymentTerms"];
    [otherDictionary2 setObject:@"" forKey:@"reserved1"];
    [otherArray addObject:otherDictionary2];
    
    [jsonDictionary setObject:otherArray forKey:@"OtherExpenses"];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSString *APIURLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buWaybillAgreement/1/submit"];
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:jsonString forKey:@"jsonString"];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [GQHNetHelper qh_requestGET:APIURLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (0 == [code integerValue]) {

            [self showToastWithText:message];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                /// 返回上两级页面
                NSUInteger index = [[self.navigationController viewControllers] indexOfObject:self];
                UIViewController *controller = [[self.navigationController viewControllers] objectAtIndex:index - 2];
                [self.navigationController popToViewController:controller animated:YES];
            });
        } else {

            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {

        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
